package com.yunchang.springboot.scheduled.amazon.mws.report.save;

import com.yunchang.springboot.base.BaseService;
import com.yunchang.springboot.models.log.BaseMwsReportLogDO;
import com.yunchang.springboot.models.log.LogReportRecord;
import com.yunchang.springboot.service.database.IBaseDatabaseService;
import com.yunchang.springboot.utils.CryptoUtil;
import com.yunchang.springboot.utils.databaseutils.DatabaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.google.common.collect.Lists.partition;

/**
 * @author czw
 */
@Slf4j
@Component
public class SaveReportData extends BaseService {

//    /**
//     * 切换数据源接口
//     */
//    @Resource
//    IBaseDatabaseService iBaseDatabaseService;

    /**
     * 一次最大插入的数据
     */
    protected static Integer INSERT_MAX_NUM = 10000;

    public synchronized void insertReportData(List insertList, BaseMwsReportLogDO mwsReportLog) {
        try {
            insertReportData(insertList);
            log.info("报告数据插入完毕!");
        } catch (Exception e) {
            log.error("插入数据时出现错误，" + e);
        }
    }

    /**
     * 将报告数据插进数据库
     * @param insertList
     */
    public void insertReportData(List insertList){
        List<Object> insertListCopy;
        if (null != insertList && !insertList.isEmpty()) {
            if (insertList.size() > INSERT_MAX_NUM) {
                // 将insertList数量大于INSERT_MAX_NUM的进行切割并返回数量为INSERT_MAX_NUM大小的集合
                List<List<Object>> subsectionResultList = partition(insertList, INSERT_MAX_NUM);
                for (int i = 0; i < subsectionResultList.size(); i++) {
                    insertListCopy = subsectionResultList.get(i);
                    baseInsertList(insertListCopy);
                }
            } else {
                baseInsertList(insertList);
            }
        } else {
            baseInsertList(insertList);
        }
    }
}
